package com.wcy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wcy.entity.Role;
import com.wcy.entity.RolePermission;
import com.wcy.mapper.RolePermissionMapper;
import com.wcy.service.IRolePermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 吴传亿
 * @since 2025-04-29
 */
@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission> implements IRolePermissionService {
    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Transactional
    @Override
    public Boolean replaceRolePermission(Role role) {
        if(role.getRoleId()!=null&&role.getPermissions()!=null&&!role.getPermissions().isEmpty()){
            rolePermissionMapper.delete(new QueryWrapper<RolePermission>().eq("role_id",role.getRoleId()));
            rolePermissionMapper.insert(role
                    .getPermissions()
                    .stream()
                    .map(e->new RolePermission(role.getRoleId(),e.getPermissionId()))
                    .collect(Collectors.toList()));
            return true;
        }
        return false;
    }
}
